<?php

namespace app\manage\controller;

use app\common\validate\system\SystemVersionValid;
use app\manage\loader\ManageServices;
use think\Exception;

/**
 * 版本控制
 */
class SystemVersion extends BaseApi
{
    /**
     * 分页
     * @throws \think\db\exception\DbException
     */
    public function   version_page(){

        $param = input('get.');
        try {
            $where = [];
            $param['app_name'] = 'manage';
            $where[] = $this->getWhere($param);
            $data = ManageServices::SystemVersion()->versionPage($where , $param['page'] ?: 1 , $param['page_size'] ?: 30);
            api_result(API_SUCCESS,'请求成功',$data);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 修改状态
     * @throws \think\db\exception\DbException
     */
    public function   version_edit_effect(){
        $param = input('post.');
        try {
            $this->validate($param,SystemVersionValid::class . '.version_edit_effect');
             ManageServices::SystemVersion()->versionEditEffect(['id' => $param['id'] , 'app_name' => 'manage'],$param['is_effect']);
            api_result(API_SUCCESS,'保存成功' , ['id' => $param['id']]);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 详情
     * @throws \think\db\exception\DbException
     */
    public function   version_info(){

        $param = input('get.');
        try {

            $where = [
                'id' => $param['id'] ?: 0,
                'version' => $param['version'] ?: '',
            ];
            if (empty($where['id'])){
                unset($where['id']);

                if (empty($where['version'])){
                   throw new Exception('版本号不能为空',API_ERROR);
                }
                $where['app_name'] = 'manage';
            }else{
                unset($where['version']);
            }

            $data =  ManageServices::SystemVersion()->versionInfo($where);
            api_result($data);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 添加/修改
     * @throws \think\db\exception\DbException
     */
    public function   version_save(){
        $param = input('post.');
        try {
            $param['app_name'] = 'manage';
            $this->validate($param,SystemVersionValid::class . '.version_save');
            $data =  ManageServices::SystemVersion()->versionSave($param);
            api_result(API_SUCCESS,'保存成功', ['id' => $data]);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 使用场景
     * @throws \think\db\exception\DbException
     */
    public function   version_scene(){
        api_result(API_SUCCESS,'保存成功', config('system.version_scene'));
    }

#==================================================
    /**
     * 分页
     * @throws \think\db\exception\DbException
     */
    public function   salesman_version_page(){

        $param = input('get.');
        try {
            $where = [];
            $param['app_name'] = 'merchants';
            $where[] = $this->getWhere($param);
            $data = ManageServices::SystemVersion()->versionPage($where , $param['page'] ?: 1 , $param['page_size'] ?: 30);
            api_result(API_SUCCESS,'请求成功',$data);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 修改状态
     * @throws \think\db\exception\DbException
     */
    public function   salesman_version_edit_effect(){
        $param = input('post.');
        try {
            $this->validate($param,SystemVersionValid::class . '.version_edit_effect');
            ManageServices::SystemVersion()->versionEditEffect(['id' => $param['id'] , 'app_name' => 'merchants'] , $param['is_effect']);
            api_result(API_SUCCESS,'保存成功' , ['id' => $param['id']]);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 详情
     * @throws \think\db\exception\DbException
     */
    public function   salesman_version_info(){

        $param = input('get.');
        try {
            $this->validate($param,SystemVersionValid::class . '.salesman_version_info');
            $where = [
                'id' => $param['id'],
                'app_name' => 'merchants',
            ];
            $data =  ManageServices::SystemVersion()->versionInfo($where);
            api_result($data);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 添加/修改
     * @throws \think\db\exception\DbException
     */
    public function   salesman_version_save(){
        $param = input('post.');
        try {
            $param['app_name'] = 'merchants';
            $this->validate($param,SystemVersionValid::class . '.version_save');
            $data =  ManageServices::SystemVersion()->versionSave($param);
            api_result(API_SUCCESS,'保存成功', ['id' => $data]);
        }catch (Exception $e){
            api_result($e);
        }
    }



    protected  function  getWhere($param){

        $where[] = ['app_name' ,'=', $param['app_name']];
        if (!empty($param['is_force'])){
            $where[] = ['is_force' ,'=', $param['is_force']];
        }
        if (!empty($param['is_transit'])){
            $where[] = ['is_transit' ,'=', $param['is_transit']];
        }
        if (!empty($param['is_effect'])){
            $where[] = ['is_effect' ,'=', $param['is_effect']];
        }
        if (!empty($param['scene'])){
            $where[] = ['scene' ,'=', $param['scene']];
        }
        return $where ?? [];
    }

}